package com.osi.project.redis.controller.oms;

import com.osi.project.config.constants.ProjectConstants;
import com.osi.project.redis.generate.pojo.RedisSlowLogDO;
import com.osi.project.redis.manager.RedisSlowLogManager;
import com.osi.project.redis.req.RedisSlowLogReqVO;
import com.osi.project.redis.resp.RedisSlowLogRespVO;
import com.osi.start.common.business.anno.Log;
import com.osi.start.common.business.constant.ValidGroup;
import com.osi.start.common.business.dto.*;
import com.osi.start.common.business.enums.ReturnCodeEnum;
import com.osi.start.common.util.bean.BeanUtil;
import com.osi.start.framework.business.service.BaseServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping(value = ProjectConstants.PATH_BASE + "/oms/redis/redisSlowLog")
@Api(tags = "Oms-redis慢sql日志相关api")
public class RedisSlowLogOmsController extends BaseServiceImpl {

    @Autowired
    private RedisSlowLogManager redisSlowLogManager;

    @Log
	@ApiOperation("创建")
	@PostMapping(value = "/create")
	@PreAuthorize("@perm.check('redis:redisSlowLog:create')")
	public RespVO<BaseVO> create(
		    @RequestBody @Validated(ValidGroup.CREATE.class) RedisSlowLogReqVO req) {

        RedisSlowLogDO redisSlowLogDO = new RedisSlowLogDO();
		BeanUtil.copyPropertiesNotNullAndNotSysField(req, redisSlowLogDO);
		redisSlowLogDO.initCreateEntitySysData(req.getReqUserId(), req.getReqUserName());
        redisSlowLogDO.setId(generateId());
        redisSlowLogManager.createSelectiveThrowException(redisSlowLogDO);
        return RespUtil.success();
	}

    @Log
	@ApiOperation("删除")
	@PostMapping(value = "/delete")
	@PreAuthorize("@perm.check('redis:redisSlowLog:delete')")
	public RespVO<BaseVO> delete(
		    @RequestBody @Valid ReqCommonIdDTO req) {
        RedisSlowLogDO redisSlowLogDO = redisSlowLogManager.findById(req.getId());
        if (redisSlowLogDO == null) {
            return RespUtil.error(ReturnCodeEnum.DATA_NONEXISTED);
        }

        redisSlowLogManager.delete(redisSlowLogDO);
		return RespUtil.success();
	}

    @Log
	@ApiOperation("修改信息")
	@PostMapping(value = "/update")
	@PreAuthorize("@perm.check('redis:redisSlowLog:update')")
	public RespVO<BaseVO> update(
		    @RequestBody @Validated(ValidGroup.UPDATE.class) RedisSlowLogReqVO req) {

        RedisSlowLogDO redisSlowLogDO = redisSlowLogManager.findById(req.getId());
        if (redisSlowLogDO == null) {
            return RespUtil.error(ReturnCodeEnum.DATA_NONEXISTED);
        }
        BeanUtil.copyPropertiesNotNullAndNotSysField(req, redisSlowLogDO);
        redisSlowLogDO.initUpdateEntitySysData(req.getReqUserId(), req.getReqUserName());
        redisSlowLogManager.updateByExampleSelectiveCheckVersionThrowException(redisSlowLogDO);
        return RespUtil.success();
	}

	@ApiOperation("查询信息")
	@PostMapping(value = "/query")
	@PreAuthorize("@perm.check('redis:redisSlowLog:query')")
	@Transactional(readOnly = true)
	public RespVO<RedisSlowLogRespVO> query(
		    @RequestBody @Validated(ValidGroup.QUERY.class) RedisSlowLogReqVO req) {
		RespVO<RedisSlowLogRespVO> respVO;
        RedisSlowLogDO redisSlowLogDO = redisSlowLogManager.findById(req.getId());
        if (redisSlowLogDO == null) {
            respVO = RespUtil.errorError(ReturnCodeEnum.DATA_NONEXISTED);
        } else {
            RedisSlowLogRespVO redisSlowLogRespVO = new RedisSlowLogRespVO();
            BeanUtil.copyProperties(redisSlowLogDO, redisSlowLogRespVO);
            respVO = RespUtil.success(redisSlowLogRespVO);
        }
        return respVO;
	}

	@ApiOperation("按条件完全匹配查找")
	@PostMapping(value = "/listEqualCondition")
	@PreAuthorize("@perm.check('redis:redisSlowLog:listEqualCondition')")
	@Transactional(readOnly = true)
	public RespVO<BasePageRespDTO<RedisSlowLogRespVO>> listEqualCondition(
			@RequestBody @Validated(ValidGroup.LIST_EQUAL_CONDITION.class) BasePageReqDTO<RedisSlowLogReqVO> req) {
        if (req.getQuery() == null) {
            req.setQuery(new RedisSlowLogReqVO());
        }
        req.setOrderBy(" id desc ");
        BasePageRespDTO<RedisSlowLogRespVO> resp = redisSlowLogManager.queryPageEqualCondition(req.getQuery(),
                req.getPageNum(), req.getPageSize(), req.getOrderBy());
        return RespUtil.success(resp);
	}

	@ApiOperation("按字符串模糊匹配和其它类型字段完全匹配查找")
	@PostMapping(value = "/listLikeCondition")
	@PreAuthorize("@perm.check('redis:redisSlowLog:listLikeCondition')")
	@Transactional(readOnly = true)
	public RespVO<BasePageRespDTO<RedisSlowLogRespVO>> listLikeCondition(
			@RequestBody @Validated(ValidGroup.LIST_LIKE_CONDITION.class) BasePageReqDTO<RedisSlowLogReqVO> req) {
        if (req.getQuery() == null) {
            req.setQuery(new RedisSlowLogReqVO());
        }
        req.setOrderBy(" id desc ");
		BasePageRespDTO<RedisSlowLogRespVO> resp = redisSlowLogManager.queryPageLikeCondition(req.getQuery(),
                req.getPageNum(), req.getPageSize(), req.getOrderBy());
        return RespUtil.success(resp);
	}

}

